package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.kitchen.domain.product.ProductType;
import org.dromara.kitchen.service.IProductTypeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商品分类
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/product/type")
public class ProductTypeController extends BaseController {

    private final IProductTypeService productTypeService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;
    /**
     * 查询商品分类列表
     */
    @SaCheckPermission("kitchen:type:list")
    @GetMapping("/list")
    public TableDataInfo<ProductType> list(ProductType productType, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        QueryWrapper<ProductType> qw = new QueryWrapper<>();

        if(productType.getParentId() != null){
            qw.eq("parent_id", productType.getParentId());
        }
        if(StrUtil.isNotEmpty(productType.getTypeName())){
            qw.like("type_name",productType.getTypeName());
        }
        qw.orderByAsc("sort");

        TableDataInfo<ProductType> td = PageUtils.buildDataInfo(productTypeService.page(PageUtils.buildPage(),qw));
        List<ProductType> list = td.getRows();
        if(CollUtil.isNotEmpty(list)){
            list.forEach(type -> {
                if(StrUtil.isNotEmpty(type.getImgUrl()) && type.getImgUrl().indexOf("http") < 0){
                    type.setImgPath(imgUrl + type.getImgUrl());
                }else{
                    type.setImgPath(type.getImgUrl());
                }
            });
        }
        return td;
    }

    /**
     * 导出商品分类列表
     */
    @SaCheckPermission("kitchen:type:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductType productType, HttpServletResponse response) {
        List<ProductType> list = productTypeService.list();
        ExcelUtil.exportExcel(list, "商品分类", ProductType.class, response);
    }


    /**
     * 商品分类列表
     */
    @GetMapping("/treeSelect")
    @Log(title = "商品分类列表")
    public R treeSelect(ProductType productType){
        List<ProductType> types = productTypeService.selectProductTypeList(productType);
        return R.ok(productTypeService.buildProductTypeTreeSelect(types));
    }


    /**
     * 商品分类列表
     */
    @GetMapping("/listAll")
//    @ApiOperation(value = "商品分类列表",notes = "商品分类列表")
    public R listAll(ProductType productType){
        List<ProductType> types = productTypeService.selectProductTypeList(productType);
        return R.ok(types);
    }

    /**
     * 获取商品分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:type:query")
    @GetMapping("/{id}")
    public R<ProductType> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        ProductType type = productTypeService.getById(id);
        if(type!=null){
            if(StrUtil.isNotEmpty(type.getImgUrl())){
                if(StrUtil.isNotEmpty(type.getImgUrl()) && type.getImgUrl().indexOf("http") < 0){
                    type.setImgPath(imgUrl + type.getImgUrl());
                }else{
                    type.setImgPath(type.getImgUrl());
                }
            }else{
                type.setImgPath("");
            }
        }
        return R.ok(type);
    }

    /**
     * 新增商品分类
     */
    @SaCheckPermission("kitchen:type:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductType productType) {
        return toAjax(productTypeService.save(productType));
    }

    /**
     * 修改商品分类
     */
    @SaCheckPermission("kitchen:type:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductType productType) {
        return toAjax(productTypeService.updateById(productType));
    }

    /**
     * 删除商品分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:type:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(productTypeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
